<?php
namespace app\controller;

use app\BaseController;
use liliuwei\think\Auth;
use EasyDingTalk\Application;
use think\facade\Db;
use think\facade\Session;
class Version extends BaseController
{
    protected $gobaluser=0;
    protected $token=0;
    public function __construct(){
        checkuser();
        $this->gobaluser=Session::get("uid");
    }
    public function index(){
        return view();
    }
    public function lists(){
        $list = getpagedata('version','',true);
		$class = r_classes(19);
	    $list[0] = $list[0]->all();
		foreach ($list[0] as $k=>$v){
			$list[0][$k]['fromID']=rclasstitle($class,$v['fromID']);
		}
	    pagejson($list[0],0,"",$list[1]);
    }

    public function push(){
        $id = request()->param("id");
		$classModel = new \app\controller\model\Classes();
        if($id>0){
			$re = r_one("version","id=$id") ;
            return view()->assign(['data'=>$re,'qudao'=>$classModel->rClassessToOption(19,$re['fromID'])]);
        }
        return view()->assign(['qudao'=>$classModel->rClassessToOption(19)]);
    }
    public function pushpost(){
        $data = request()->param();
        unset($data['id']);
        $id = request()->param("id");
        !isset($data['num']) || $data['num']==''?echojson([],1,'版本号必填'):'';
        !isset($data['os']) || $data['os']==''?echojson([],1,'系统必选'):'';
        !isset($data['version']) || $data['version']==''?echojson([],1,'版本信息必填'):'';
        !isset($data['url']) || $data['url']==''?echojson([],1,'url链接必填'):'';
        !isset($data['content']) || $data['content']==''?echojson([],1,'更新说明不能为空'):'';
        $data['addtime']=date("Y-m-d H:i:s");
		if($data['os']=='android' && $data['fromID']<1){
//			echojson([],1,'安卓渠道必选');
		}
        if($id>0){
            $re = r_one("version","id=$id");
            if($re){
                DB::name("version")->where("id",$id)->update($data);
                echojson([],0,'更新成功');
            }else{
                echojson([],1,'ID错误');
            }
        }else{
            $re = DB::name("version")->insert($data);
            if($re){
                echojson([],0,'添加成功');
            }else{
                echojson([],1,'ID错误');
            }
        }
    }
}
